package com.et.core.file

import android.util.Log
import com.et.core.goods.TAG
import com.et.core.store.DataBaseUtil
import com.et.core.store.FileEntity
import com.et.core.store.IFileDao
import com.et.core.store.NORMAL
import com.et.logger.ELog
import com.et.logger.TAGS
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers

interface IAdvTrim {
    fun add(apply: FileEntity)
    fun remove(apply: FileEntity)
    fun clear()
    fun update(apply: FileEntity)
}

val globalFileEntityMap = HashMap<String, FileEntity>()

class AdvHelper : IAdvTrim {
    private var disposable: Disposable? = null

    /**
     * call by user
     */
    fun subscribe() {
        if (disposable == null) {
            Log.d(TAGS.machine, "subscribe  advert info change")
            val channelBox = DataBaseUtil.IFileBox<IFileDao>()
            if (DataBaseUtil.fileDao(channelBox) == NORMAL) {
                channelBox.dao?.let { it ->
                    disposable = it.all()
                        .subscribeOn(Schedulers.io())
                        .subscribe { channelList ->
                            channelList.forEach {
                                Log.d(TAGS.vending, it.toString())
                                globalFileEntityMap[it.name] = it

                            }
                        }

                }
            }
        }
    }

    /**
     * call by com.et.core.release, user ignored
     */
    fun unsubscribe() {
        disposable?.dispose()
    }

    companion object {

        @JvmStatic
        fun getInstance() = Holder.holder
    }

    object Holder {
        val holder = AdvHelper()
    }

    override fun add(apply: FileEntity) {
        val box = DataBaseUtil.IFileBox<IFileDao>()
        if (DataBaseUtil.fileDao(box) == NORMAL) {
            box.dao?.also { dao ->
                dao.insert(apply).subscribeOn(Schedulers.io()).subscribe()
            }
        }
    }


    override fun remove(apply: FileEntity) {
        val box = DataBaseUtil.IFileBox<IFileDao>()
        if (DataBaseUtil.fileDao(box) == NORMAL) {
            box.dao?.also {
                it.delete(apply).subscribeOn(Schedulers.io()).subscribe()
            }
        }
    }

    override fun clear() {
        val box = DataBaseUtil.IFileBox<IFileDao>()
        if (DataBaseUtil.fileDao(box) == NORMAL) {
            box.dao?.also {
                it.deleteAll().subscribeOn(Schedulers.io()).subscribe()
            }
        }
    }


    override fun update(apply: FileEntity) {
        val box = DataBaseUtil.IFileBox<IFileDao>()
        if (DataBaseUtil.fileDao(box) == NORMAL) {
            box.dao?.also {
                it.update(apply).subscribeOn(Schedulers.io()).subscribe()
            }
        }
    }

    /**
     * for test cat database
     */
    fun cat() {
        ELog.d(TAGS.vending, "vending:test cat database")
        val box = DataBaseUtil.IFileBox<IFileDao>()
        if (DataBaseUtil.fileDao(box) == NORMAL) {
            box.dao?.also { it ->
                it.all().subscribe { it1 ->
                    ELog.d(TAGS.vending, "vending:channel entity size = ${it1.size}")
                    it1.forEach {
                        Log.d(TAGS.vending, it.toString())
                    }
                }
            }
        }
    }
}
